/**
 * 
 */
package de.cbf.cam.map2d.listeners;

import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Scrollable;
import org.eclipse.swt.widgets.Widget;

/**
 * @author Stefan Alter
 *
 */
public class GridPaintListener implements PaintListener {
	
	private float zoomFactor;
	private int distanceHorizontal=30;
	private int distanceVertical=30;
	
	/**
	 * 
	 */
	public GridPaintListener(float zoomFactor) {
		this.zoomFactor = zoomFactor;
	}

	@Override
	public void paintControl(PaintEvent event) {
		Widget widget =event.widget;
		if (widget instanceof Scrollable) {
			Scrollable scrollable = (Scrollable) widget;
			Rectangle clientArea = scrollable.getClientArea();
			drawGrid(event.gc, clientArea);
		}		 
	}
	
	private void drawGrid(GC gc,Rectangle clientArea){
		//vertical lines
		for(int i=0;i*distanceHorizontal*zoomFactor<clientArea.width;i++){
			float floatLineX=distanceHorizontal*i*zoomFactor;
			int lineX =Math.round(floatLineX);
			gc.drawLine(lineX,0,lineX,clientArea.height);
		}
		//horizontal lines
		for(int i=0;i*distanceVertical*zoomFactor<clientArea.height;i++){
			float floatLineY=distanceVertical*i*zoomFactor;	
			int lineY = Math.round(floatLineY);
			gc.drawLine(0,lineY,clientArea.width,lineY);
		}		
	}
	
	public float getZoomFactor() {
		return zoomFactor;
	}
	
	public int getDistanceHorizontal() {
		return distanceHorizontal;
	}
	
	public int getDistanceVertical() {
		return distanceVertical;
	}
	
	public void setZoomFactor(float zoomFactor) {
		this.zoomFactor = zoomFactor;		
	}
	
	public void setDistanceHorizontal(int distanceHorizontal) {
		this.distanceHorizontal = distanceHorizontal;
	}

	public void setDistanceVertical(int distanceVertical) {
		this.distanceVertical = distanceVertical;
	}


}
